package com.a0soft.gphone.aTruffleHog.scrsht;
/*
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 3, or (at your option) any
 * later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * Copyright(C) 2011 
 *   Sam Lu <ysl.sam.lu (at) gmail.com>
 *
 */

import java.io.File;

import com.a0soft.gphone.aTruffleHog.R;
import com.a0soft.gphone.aTruffleHog.gio.ui.BaseActivity;
import com.a0soft.gphone.aTruffleHog.util.aDebug;

import android.content.Intent;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class ScreenshotShareWnd extends BaseActivity {
	public final static String KEY_FILENAME = ScreenshotShareWnd.class.getName() + ".fn";
	
	private String	m_strFilename;	
	
	private final static String LOG_TAG = ScreenshotShareWnd.class.getSimpleName();
	
	@Override protected void 
	onCreate(Bundle bdSavedInstanceState)
	{
		super.onCreate(bdSavedInstanceState);
		
		Bundle bd = getIntent().getExtras();
		if (bd == null)
			bd = bdSavedInstanceState;
		if (bd == null) {
			aDebug.d(LOG_TAG, "bundle is NULL!");
			finish();
			return;
		}
		
		m_strFilename = bd.getString(KEY_FILENAME);
		if (TextUtils.isEmpty(m_strFilename)) {
		    if (aDebug.ENABLED)
		        aDebug.e(LOG_TAG, "strFilename is NULL!");
			finish();
			return;
		}
		if (aDebug.ENABLED)
            aDebug.d(LOG_TAG, "strFilename=" + m_strFilename);
		
		setContentView(R.layout.screenshot_share_wnd);
		
		getActivityHelper().setupActionBar(getTitle(), 0);

		ImageView iv = (ImageView)findViewById(R.id.preview);
		iv.setImageBitmap(BitmapFactory.decodeFile(m_strFilename));
		
		TextView tvDesc = (TextView)findViewById(R.id.desc);
		String str = getString(R.string.screenshot_save_to_sd, m_strFilename);
		tvDesc.setText(str);
	}
	
	@Override protected void 
	onSaveInstanceState(Bundle outState)
	{
		super.onSaveInstanceState(outState);
		
		outState.putString(KEY_FILENAME, m_strFilename);
	}

	@Override public boolean
    onCreateOptionsMenu(Menu menu) 
    {
        if (aDebug.ENABLED) aDebug.d(LOG_TAG, "onCreateOptionsMenu()");
        getMenuInflater().inflate(R.menu.screenshot_share_wnd, menu);
        return super.onCreateOptionsMenu(menu);
    }
    
    @Override public boolean 
    onOptionsItemSelected(MenuItem item)
    {
        if (aDebug.ENABLED) aDebug.d(LOG_TAG, "onOptionsItemSelected()");
        final int ID = item.getItemId();
        if (ID == R.id.menu_share) {
            _Share();
            return true;
        }
        else if (ID == R.id.menu_del) {
            if (_Delete())
                finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
    
	private boolean
	_Delete()
	{
		File file = new File(m_strFilename);
		if (file.exists()) {
			if (!file.delete()) {
				Toast.makeText(this, 
						getString(R.string.screenshot_del_error, m_strFilename), 
						Toast.LENGTH_LONG)
					 .show();
				return false;
			}
		}
		return true;
	}
	
	private void
	_Share()
	{
		Intent it = new Intent(Intent.ACTION_SEND);
		it.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
		it.setType("image/jpeg");
		it.putExtra(Intent.EXTRA_SUBJECT, getString(R.string.screenshot_share_subject));
		it.putExtra(Intent.EXTRA_TEXT, getString(R.string.screenshot_share_content));
		it.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(m_strFilename)));
		startActivity(it);
	}
}
